import React,{Component} from 'react';
import { Spin,message} from 'antd';
import * as pub from '../../config/public';
import * as fetch from '../../config/fetch';
import '../../style/less/privateLetter.less';
import Pages from '../Pages';
class PrivateDialogue extends Component {
	constructor(args) { 
		super()
		this.state = {
			senderRealName:'',
			letterTheme:'',
			senderNamePath:'',
			addressNamePath:'',
			privateLetterInfoRusult:[],
			addressRealName:'',
			textareaValue:'',
			infoSuccess:false,
			jumpPage: 1,
			totalPage: 1,
		}
	}
	show(){
		let data = this.props.location.state
		let body = {}
		if(data.infoSuccess === true || this.state.infoSuccess === true){
			body={privateletterId:this.props.params.id,changType:'1',start:this.state.jumpPage,userType:JSON.parse(localStorage.getItem('user')).userType}
		}else{
			body={privateletterId:this.props.params.id,start:this.state.jumpPage,userType:JSON.parse(localStorage.getItem('user')).userType}
		}
		pub.loading(this,true);
		fetch.letterinfo(body).then((data)=>{
			if(data.result === 'RC100'){
				this.setState({
					senderRealName:data.senderRealName,
					letterTheme:data.letter,
					senderNamePath:data.senderNamePath,
					privateLetterInfoRusult:data.privateLetterInfoRusult,
					addressNamePath:data.addressNamePath,
					addressRealName:data.addressRealName,
					totalPage:data.totalPage
				})
			}else if(data.result === 'RC101'){
				this.setState({
					letterTheme:data.letter,
					addressRealName:data.addressRealName,
					senderRealName:data.senderRealName,
				})
			}
			else{
				message.error(data.errMsg, 3);
			}
			pub.loading(this,false);
		})
	}
	componentWillMount(){
		this.show()
	}
	sendLetter(){
		if(this.refs.content.value === ''){
			message.error('发送内容不能为空', 3);
			return
		}
		let formData = new FormData()
		formData.append('privateletterInfoNote',this.refs.content.value)
		formData.append('privateletterId',this.props.params.id)
		formData.append('userType',JSON.parse(localStorage.getItem('user')).userType)
		pub.loading(this,true);
		fetch.sendLetterInfo(formData).then((data)=>{
			//console.log(data)
			if(data.result === 'RC100'){
				this.setState({
					textareaValue:'',
					infoSuccess:true
				})
				this.show()
			}else{
				this.setSate({
					infoSuccess:false
				})
				message.error(data.errMsg, 3);	
			}
			pub.loading(this,false);
		})
	}
	jump(page) {
		this.setState({
			jumpPage: page
		}, () => {
			this.show();
		})
	}
	render(){
		let letterTheme = this.state.letterTheme
		let senderNamePath = this.state.senderNamePath 
		let addressNamePath = this.state.addressNamePath
		let now_userType = localStorage.getItem('now_userType')
		let userCode  = pub.user.userCode
		return(
			<div className="wrapper bg-F0F4F6 privateLetter">
				<div className="layout clearFix pad-t-20">
					
					<div className="row common-title" style={{padding: '10px 30px'}}>私信
					    <button className="sx-refreshs" onClick={()=>{this.show()}}>刷新</button>
						<button className="sx-refresh" onClick={()=>{window.history.back()}}>返回</button>
					</div>
					<Spin spinning={this.state.loading} tip="加载列表中...">
						<div className="talk-container">
						{
							now_userType === '学员' ? 
								<div className="tip">您正在和{this.state.addressRealName}私信</div>
							:
								<div className="tip">您正在和{this.state.senderRealName}私信</div>
						}
							<div className="talk-theme">
								<p className="title">消息主题:</p>
								{
									letterTheme !== null ?
										<div className="talk-cont">
											<p className="time">{pub.formatTimestamp(letterTheme.createTime)}</p>
											<div className="pepole-talk">
											<div className="avatar"><img alt='test' src={pub.getFile(addressNamePath)} onError={(e) => pub.headImageError(e)}/></div>
												<div className="name-txt">
                                                      <p>{this.state.senderRealName}</p>    
													<div className="talk-word">{letterTheme.privateletterNote}</div>
												</div>
											</div>
										</div>
									:null
								}
							</div>
							<div className="talk-notes">
								{
									this.state.privateLetterInfoRusult.map((item,index)=>{
										return(
											<div className="talk-cont" key={index}>
									
									<p className="time">{pub.formatTimestamp(item.createTime)}</p>
												{/*{
													item.createUser === this.state.letterTheme.addresseeName ? 
														<div className="pepole-talk">
															<div className="avatar"><img src={pub.getFile(addressNamePath)} onError={(e) => pub.headImageError(e)} alt='test'/></div>
															<div className="name-txt">
																<p>{this.state.addressRealName}讲师</p>
																<div className="talk-word">{item.privateletterInfoNote}</div>
															</div>
														</div>
													:
														<div className="pepole-talk me-talk">
															<div className="avatar"><img src={pub.getFile(senderNamePath)} onError={(e) => pub.headImageError(e)} alt='test'/></div>
															<div className="name-txt">
																<p>{this.state.senderRealName}</p>
																<div className="talk-word">{item.privateletterInfoNote}</div>
															</div>
														</div>
												}	*/}
												{
													
													item.createUser === userCode ? 
														<div className="pepole-talk me-talk">
															<div className="avatar"><img src={pub.getFile(addressNamePath)} onError={(e) => pub.headImageError(e)} alt='test'/></div>
															<div className="name-txt">
																<p>我</p>
																<div className="talk-word">{item.privateletterInfoNote}</div>
															</div>
														</div>
													:
														<div className="pepole-talk">
															<div className="avatar"><img src={pub.getFile(senderNamePath)} onError={(e) => pub.headImageError(e)} alt='test'/></div>
															<div className="name-txt">
																{
																	now_userType === '学员' ? 
																		<p>{this.state.addressRealName}讲师</p>
																	:
																		<p>{this.state.senderRealName}</p>
																}
																
																<div className="talk-word">{item.privateletterInfoNote}</div>
															</div>
														</div>
												}
											</div>
										)	
								})}	
							</div>
							<div className="talk-textarea">
								<textarea placeholder="请输入..." required="required" ref='content' value={this.state.textareaValue} onChange={()=>{this.setState({textareaValue:this.refs.content.value})}}></textarea>
								<button type="button" onClick={()=>this.sendLetter()}>发送</button>
							</div>
						</div>
						<div className="line"></div>
					<Pages totalPage={this.state.totalPage} changePage={this.state.jumpPage} jump={this.jump.bind(this)}/>
					</Spin>	
				</div>
			</div>
		)
	}
	
}



export default PrivateDialogue